package com.ruiyian.bridge.project.controller;

import com.ruiyian.bridge.info.service.DistanceInfoService;
import com.ruiyian.bridge.info.service.Mmh2oInfoService;
import com.ruiyian.bridge.project.domain.BridgeInfo;
import com.ruiyian.bridge.project.domain.MeasurePoint;
import com.ruiyian.bridge.project.domain.vo.BridgeSummaryVo;
import com.ruiyian.bridge.project.domain.vo.DataAnalysisDto;
import com.ruiyian.bridge.project.domain.vo.Mmh2oDayQueryBo;
import com.ruiyian.bridge.project.domain.vo.Mmh2oQueryBo;
import com.ruiyian.bridge.project.service.BridgeInfoService;
import com.ruiyian.bridge.project.service.MeasurePointfoService;
import com.ruiyian.common.annotation.Log;
import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.page.TableDataInfo;
import com.ruiyian.common.enums.BusinessType;
import com.ruiyian.common.utils.DateUtils;
import com.ruiyian.common.utils.SecurityUtils;
import com.ruiyian.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 测点信息Controller
 *
 * @author ruiyian
 * @date 2022-01-20
 */
@RestController
@RequestMapping("/bridge/project/sensor")
public class MeasurePointController extends BaseController
{
    @Autowired
    private MeasurePointfoService tMeasurePointfoService;

    @Autowired
    private BridgeInfoService bridgeInfoService;
    @Autowired
    private Mmh2oInfoService mmh2oInfoService;
    @Autowired
    private DistanceInfoService distanceInfoService;



    /**
     * 查询测点信息列表分页
     */
    @GetMapping("/list")
    public TableDataInfo list(MeasurePoint measurePoint)
    {
        startPage();
        List<MeasurePoint> list = tMeasurePointfoService.selectTSensorInfoList(measurePoint);
        return getDataTable(list);
    }

    /**
     * 导出测点信息列表
     */
    @Log(title = "测点信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeasurePoint measurePoint)
    {
        List<MeasurePoint> list = tMeasurePointfoService.selectTSensorInfoList(measurePoint);
        ExcelUtil<MeasurePoint> util = new ExcelUtil<MeasurePoint>(MeasurePoint.class);
        try {
            util.exportExcel(response, list, "测点信息数据");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取测点信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tMeasurePointfoService.selectTSensorInfoById(id));
    }

    /**
     * 所有数据
     * @param measurePoint
     * @return
     */
    @PostMapping(value="/getSensorList")
    public AjaxResult query(@RequestBody MeasurePoint measurePoint) {
        List<MeasurePoint> list = tMeasurePointfoService.selectTSensorTypeList(measurePoint);
        return AjaxResult.success(list);

    }

    /**
     * 新增测点信息
     */
    @Log(title = "测点信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MeasurePoint measurePoint)
    {
        return toAjax(tMeasurePointfoService.insertTSensorInfo(measurePoint));
    }

    /**
     * 修改测点信息
     */
    @Log(title = "测点信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeasurePoint measurePoint)
    {
        //获取桥梁id
        BridgeInfo bridgeInfo = bridgeInfoService.selectTBridgeInfoById(measurePoint.getBridgeId());
        measurePoint.setBridgeName(bridgeInfo.getBridgeName());
        System.out.println(bridgeInfo.getBridgeName());
        return toAjax(tMeasurePointfoService.updateTSensorInfo(measurePoint));
    }

    /**
     * 删除测点信息
     */
    @Log(title = "测点信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tMeasurePointfoService.deleteTSensorInfoByIds(ids));
    }

    /**
     * 根据桥梁id获取测点信息
     * @param bridgeId
     * @return
     */
    @PostMapping("/getMeasPointList")
    public AjaxResult getMeasPointList(@RequestBody Long bridgeId)
    {
       return AjaxResult.success(tMeasurePointfoService.getMeasPointList(bridgeId));
    }

    /**
     * 获取所有桥梁测点信息
     * @return
     */
    @GetMapping("/getMeasPointListAll")
    public AjaxResult getMeasPointList()
    {
        List list = new ArrayList();
        List<BridgeInfo> bridgeInfos = bridgeInfoService.selectTBridgeInfoList(new BridgeInfo());
        for(BridgeInfo b: bridgeInfos){
            List<Map<String, Object>> measPointList = tMeasurePointfoService.getMeasPointList(b.getId());
            list.addAll(measPointList);
        }
        return AjaxResult.success(list);
    }



    /**
     * 根据编号获取测点数据
     * @param mmh2oQueryBo
     * @return
     */
    @PostMapping("/getAllTypePointData")
    public AjaxResult getMeasPointList(@RequestBody Mmh2oQueryBo mmh2oQueryBo)
    {
        if(StringUtils.isEmpty(mmh2oQueryBo.getSearchTime())){
            mmh2oQueryBo.setSearchTime(DateUtils.getDate());
        }
        return AjaxResult.success(tMeasurePointfoService.queryDataListByAddr(mmh2oQueryBo));
    }

    /**
     * 数据查看
     * @param dto
     * @return
     */
    @PostMapping(value = "/lookData")
    public AjaxResult lookData(@RequestBody DataAnalysisDto dto) {
        Map<String, Object> map = tMeasurePointfoService.lookData(dto);
        return AjaxResult.success(map);
    }

    /**
     * 桥梁分组统计静力传感器
     * @param bo
     * @return
     */
    @PostMapping(value = "/countSummaryMmh")
    public AjaxResult queryPageListByBridgeList(@RequestBody Mmh2oDayQueryBo bo) {
        List<BridgeSummaryVo> list = mmh2oInfoService.queryPageListByBridgeList(bo);
        Map<String,List<BridgeSummaryVo>> map = new TreeMap<>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        if (CollectionUtils.isEmpty(list)){
            return AjaxResult.success(map);
        }
        list.forEach(item -> {
            List<BridgeSummaryVo> branchList = map.getOrDefault(item.getCreatTime(), new ArrayList<BridgeSummaryVo>());
            branchList.add(item);
            map.put(item.getCreatTime(), branchList);
        });
        return AjaxResult.success(map);
    }

    /**
     * 桥梁分组统计激光位移
     * @param bo
     * @return
     */
    @PostMapping(value = "/countSummaryDistance")
    public AjaxResult countSummaryDistance(@RequestBody Mmh2oDayQueryBo bo) {
        List<BridgeSummaryVo> list = distanceInfoService.queryPageListByBridgeList(bo);
        Map<String,List<BridgeSummaryVo>> map = new TreeMap<>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        if (CollectionUtils.isEmpty(list)){
            return AjaxResult.success(map);
        }
        list.forEach(item -> {
            List<BridgeSummaryVo> branchList = map.getOrDefault(item.getCreatTime(), new ArrayList<BridgeSummaryVo>());
            branchList.add(item);
            map.put(item.getCreatTime(), branchList);
        });
        return AjaxResult.success(map);
    }


    /**
     * 按照桥墩统计
     * @param bo
     * @return
     */
    @PostMapping(value = "/countListByPier")
    public AjaxResult countListByPier(@RequestBody Mmh2oDayQueryBo bo) {
        List data = new ArrayList();
        List<BridgeInfo> bridgeInfos = bridgeInfoService.selectTBridgeInfoList(new BridgeInfo());
        for(BridgeInfo b: bridgeInfos){
            List<String> category = new ArrayList<>();
            List<String> seriesMmh = new ArrayList<>();
            List<String> seriesDis = new ArrayList<>();
            Map<String,Object> mapData = new HashMap<>();
            bo.setBridgeId(b.getId());
            List<BridgeSummaryVo> list = mmh2oInfoService.queryPageListBypier(bo);
            Map<String,List<BridgeSummaryVo>> map = new TreeMap<>(new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return o1.compareTo(o2);
                }
            });
            list.forEach(item -> {
                List<BridgeSummaryVo> branchList = map.getOrDefault(item.getCreatTime(), new ArrayList<BridgeSummaryVo>());
                branchList.add(item);
                map.put(item.getCreatTime(), branchList);
                category.add(item.getCreatTime());
                seriesMmh.add(branchList.size()>0?branchList.get(0).getMmh2o():"0");
            });
            List<BridgeSummaryVo> dislist = distanceInfoService.queryPageListByPier(bo);
            for(String str: category){
                List<BridgeSummaryVo> collect = dislist.stream().filter(p -> org.apache.commons.lang3.StringUtils.isNotBlank(p.getCreatTime()) && p.getCreatTime().equals(str)).collect(Collectors.toList());
                if(collect.size()>0){
                    seriesDis.add(collect.get(0).getDiffValue());
                }else {
                    seriesDis.add("0");
                }
            }
            mapData.put("category",category);
            mapData.put("seriesMmh",seriesMmh);
            mapData.put("seriesDis",seriesDis);
            data.add(mapData);
        }
        return AjaxResult.success(data);
    }
}
